//
//  ContactViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/6.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire

class ContactViewController: UIViewController {
    var ContactArray = [ContactModel]()
    
    
    var bigContactArray = [ContactModel]()
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    
    var index = 0
    
    
    var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = bgGrayColor;
        
        navigationItem.title="联系我们"
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        //tableView.rowHeight=50
        //tableView.separatorInset = UIEdgeInsets.zero
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        
         tableView.register(NSClassFromString("ContactTableViewCell"), forCellReuseIdentifier: "ContactTableViewCell")
        
        view.addSubview(tableView)
        
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        
        
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        // 现在的版本要用mj_header
        tableView.mj_header = header
        
        tableView.mj_header.beginRefreshing()
        
//        // 上拉刷新
//        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
//        tableView.mj_footer = footer
//        
//        
//       self.loadData()
    }
    
    // 顶部刷新
    @objc func headerRefresh(){
        
        self.ContactArray.removeAll()
        self.bigContactArray.removeAll()
        
        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/contactUs/getContactUs"
        
        let url = URL(string: urlString)!
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        
        
        Alamofire.request(request).responseJSON {
            (response) in
            
       
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array  = json["resultData"].array;
                
                for dataDic in  array!
                {
                    let model =   ContactModel()
                    
                    model.address = dataDic["address"].string
                    model.company = dataDic["company"].string
                    model.email = dataDic["email"].string
                    model.id = dataDic["id"].string
                    model.latitude = dataDic["latitude"].string
                    model.longitude = dataDic["longitude"].string
                    model.tel = dataDic["tel"].string
                    
                    self.ContactArray.append(model)
                    
                }
                
                print("--------\(self.ContactArray.count)")
                
                self.bigContactArray.append(contentsOf: self.ContactArray)
                
   
                self.tableView.reloadData()
                
                self.tableView.mj_header.endRefreshing()
              
            }else{
                self.tableView.mj_header.endRefreshing()
            }
            
          
            
        }
        
        
    }
    
    // 顶部刷新
    @objc func footerRefresh(){
        
      
        
        
        
    }
    
    func loadData() {
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/contactUs/getContactUs"
       
        let url = URL(string: urlString)!
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
       
        
        Alamofire.request(request).responseJSON {
            (response) in
            
            print("=======\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array  = json["resultData"].array;
                
                for dataDic in  array!
                {
                    let model =   ContactModel()
                    
                    model.address = dataDic["address"].string
                    model.company = dataDic["company"].string
                    model.email = dataDic["email"].string
                    model.id = dataDic["id"].string
                    model.latitude = dataDic["latitude"].string
                    model.longitude = dataDic["longitude"].string
                    model.tel = dataDic["tel"].string
                    
                    self.ContactArray.append(model)
                    
                }
    
                print("--------\(self.ContactArray.count)")
            }
            
            self.tableView.reloadData()
            
        }
        
    }



}

extension ContactViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.bigContactArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 251
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = ContactTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "ContactTableViewCell")
        let cellModel:ContactModel = self.bigContactArray[indexPath.row]
  
        cell.showAppInfoWithModel(model: cellModel)

        return cell
    }
    
}
